package IC.TypeTable;

import java.util.*;
///////////////// CHECK THIS//////////////////////
public class MethodType extends Type {

	private Type returnType;
	private List<Type> listTypes;  //parameter_Types

	public MethodType(Type returnType, List<Type> listTypes)
	{
		super(null);
		this.listTypes = listTypes;
		this.returnType = returnType;
	}

	public List<Type> getListTypes(){
		return this.listTypes;
	}

	public Type getReturnType(){
		return this.returnType;
	}

	// name was changed
	public boolean isEqual(MethodType method_type)
	{
		// compare .getName()
		if (this.getName() != method_type.getName())
		{
			return false;
		} 

		// compare .getReturnType()
		if (this.returnType != method_type.getReturnType())
		{
			return false;
		}

		// check that both lists have the same size
		if (this.listTypes.size()==this.listTypes.size() )
		{
			//  two lists have the same size --> compare .listTypes type by type
			Iterator<Type> iter1 = this.listTypes.iterator();
			Iterator<Type> iter2 = method_type.listTypes.iterator();

			//while there are types in both of the lists
			while (iter1.hasNext() && iter2.hasNext())
			{
				if (iter1.next() != iter2.next())
				{
					return false; 
				}
			}
			return true;	
		}
		else
			return false;
	}

	public boolean isSubTypeOf(Type t)
	{
		return (t == this);
	}

	public String toString(){
		
		String listTypesStr="";
		
		// list of types
		Iterator<Type> iter = listTypes.iterator();
		// check  if listTypes is not empty
		if (iter.hasNext()) 
		{
			listTypesStr += iter.next().getName(); 	
			while (iter.hasNext())
				listTypesStr += ", " + iter.next().getName(); 
		}
		
		return "{" + listTypesStr + " -> " + this.returnType.getName() + "}" ;
	}
}